package com.monkeys.service.impl;

import com.monkeys.Utils.CalculateUtil;
import com.monkeys.Utils.RoutesUtil;
import com.monkeys.mapper.GoodsDemandMapper;
import com.monkeys.pojo.DemandsList;
import com.monkeys.pojo.GoodsDemand;
import com.monkeys.pojo.Poi;
import com.monkeys.pojo.Vehicle;
import com.monkeys.service.DemandsListService;
import com.monkeys.service.GoodsDemandService;
import com.monkeys.service.PoiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author KuangTeam
 */
@Service
@RequiredArgsConstructor
public class GoodsDemandServiceImpl implements GoodsDemandService {

    private final GoodsDemandMapper goodsDemandMapper;
    private final PoiService poiService;
    private final DemandsListService demandsListService;

    //根据id获取订单
    @Override
    public GoodsDemand getById(Long goodsDemandId) {
        return goodsDemandMapper.getById(goodsDemandId);
    }


    //根据id获取该订单的发送方的距离
    @Override
    public Double getSenderDistanceById(Vehicle vehicle, Long demandId) {
        GoodsDemand goodsDemand = getById(demandId);
        Poi senderPoi = poiService.getById(goodsDemand.getSenderId());

        String vehicleLatLng = CalculateUtil.turnLngLatToString(vehicle.getLat(), vehicle.getLng());
        String poiLatLng = CalculateUtil.turnLngLatToString(senderPoi.getLat(), senderPoi.getLng());

        return RoutesUtil.getDistance(vehicleLatLng, poiLatLng);
    }


    //根据id获取该订单的目的地的距离
    @Override
    public Double getOwnerDistanceById(Long demandId) {
        GoodsDemand goodsDemand = getById(demandId);
        Poi senderPoi = poiService.getById(goodsDemand.getSenderId());
        Poi ownerPoi = poiService.getById(goodsDemand.getOwnerId());

        String senderPoiLatLng = CalculateUtil.turnLngLatToString(senderPoi.getLat(), senderPoi.getLng());
        String ownerPoiLatLng = CalculateUtil.turnLngLatToString(ownerPoi.getLat(), ownerPoi.getLng());

        return RoutesUtil.getDistance(senderPoiLatLng, ownerPoiLatLng);
    }


    //获取到发送方的路线关键点经纬度集合（如"34.123 , 110.124"）
    @Override
    public String[] getSenderLngLatsById(Vehicle vehicle, Long demandId) {
        GoodsDemand goodsDemand = getById(demandId);
        Poi senderPoi = poiService.getById(goodsDemand.getSenderId());

        String vehicleLatLng = CalculateUtil.turnLngLatToString(vehicle.getLat(), vehicle.getLng());
        String poiLatLng = CalculateUtil.turnLngLatToString(senderPoi.getLat(), senderPoi.getLng());

        return RoutesUtil.getLngLatList(vehicleLatLng, poiLatLng);
    }


    //获取到基地的路线关键点经纬度集合（如"34.123 , 110.124"）（重载）
    @Override
    public String[] getSenderLngLatsById(Vehicle vehicle, Double primaryLng, Double primaryLat) {
        String vehicleLatLng = CalculateUtil.turnLngLatToString(vehicle.getLat(), vehicle.getLng());
        String homeLatLng = CalculateUtil.turnLngLatToString(primaryLat, primaryLng);

        return RoutesUtil.getLngLatList(vehicleLatLng, homeLatLng);
    }


    //获取到目的地的路线关键点经纬度集合（如"34.123 , 110.124"）
    @Override
    public String[] getOwnerLngLatsById(Long demandId) {
        GoodsDemand goodsDemand = getById(demandId);
        Poi senderPoi = poiService.getById(goodsDemand.getSenderId());
        Poi ownerPoi = poiService.getById(goodsDemand.getOwnerId());

        String senderPoiLatLng = CalculateUtil.turnLngLatToString(senderPoi.getLat(), senderPoi.getLng());
        String ownerPoiLatLng = CalculateUtil.turnLngLatToString(ownerPoi.getLat(), ownerPoi.getLng());

        return RoutesUtil.getLngLatList(senderPoiLatLng, ownerPoiLatLng);
    }


    //获取到目的地的lng
    @Override
    public Double getOwnerLngById(Long demandId) {
        GoodsDemand goodsDemand = getById(demandId);
        Poi ownerPoi = poiService.getById(goodsDemand.getOwnerId());
        return ownerPoi.getLng();
    }


    //获取到目的地的lat
    @Override
    public Double getOwnerLatById(Long demandId) {
        GoodsDemand goodsDemand = getById(demandId);
        Poi senderPoi = poiService.getById(goodsDemand.getSenderId());
        return senderPoi.getLat();
    }


    //计算车辆模拟完后回基地的距离
    @Override
    public Double goBackGetDistance(Vehicle vehicle, Double primaryLng, Double primaryLat) {
        //获取最后一个订单信息
        DemandsList demandsList = demandsListService.getById(vehicle.getDemandsListId());
        List<Long> demandsListIds = CalculateUtil.getDemandsListIdList(demandsList.getDemandsListStr());
        Long lastDemandId = demandsListIds.get(demandsListIds.size() - 2);
        GoodsDemand goodsDemand = getById(lastDemandId);

        //获取最后一个订单的目的地，这里就是车辆最后的地点
        Poi vehiclePoi = poiService.getById(goodsDemand.getOwnerId());
        String vehicleLatLngStr = CalculateUtil.turnLngLatToString(vehiclePoi.getLat(), vehiclePoi.getLng());
        String primaryLatLngStr = CalculateUtil.turnLngLatToString(primaryLat, primaryLng);

        return RoutesUtil.getDistance(vehicleLatLngStr, primaryLatLngStr);
    }



}
